package com.bos.controller.json;

import com.bos.dao.KabupatenDAO;
import com.bos.dao.KecamatanDAO;
import com.bos.domain.Kabupaten;
import com.bos.domain.Kecamatan;
import com.bos.editor.KabupatenEditor;
import com.bos.editor.KecamatanEditor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by IntelliJ IDEA.
 * User: Latief
 * Date: 3/18/11
 * Time: 3:06 PM
 * To change this template use File | Settings | File Templates.
 */
@Controller
public class KecamatanJsonController {

    private KecamatanDAO kecamatanDAO;
    private KabupatenDAO kabupatenDAO;

/* --------------------- Getter and Setter ---------------------*/

    @Autowired
    public void setKecamatanDAO(KecamatanDAO kecamatanDAO) {
        this.kecamatanDAO = kecamatanDAO;
    }

    public KabupatenDAO getKabupatenDAO() {
        return kabupatenDAO;
    }

    @Autowired
    public void setKabupatenDAO(KabupatenDAO kabupatenDAO) {
        this.kabupatenDAO = kabupatenDAO;
    }

    /**
     * ************** JSON **************************************************************************
     */

    @RequestMapping(value = "/kecamatan.json", method = RequestMethod.GET)
    public
    @ResponseBody
    List<Kecamatan> index() {
        try {
            return kecamatanDAO.getAll();
        } catch (DataAccessException e) {
            return new ArrayList<Kecamatan>();
        }
    }

    @RequestMapping(value = "/kecamatan/{id}.json", method = RequestMethod.GET)
    public
    @ResponseBody
    Kecamatan show(@PathVariable("id") Kecamatan kecamatan) {
        return kecamatan;
    }

    @RequestMapping(value = "/kecamatan/kabupaten/{id}.json", method = RequestMethod.GET)
    public
    @ResponseBody
    List<Kecamatan> getByKabupaten(@PathVariable("id") Kabupaten kabupaten) {
        List<Kecamatan> kecamatans = new ArrayList<Kecamatan>();
        try {
            kecamatans = kecamatanDAO.getByKabupaten(kabupaten);
        } catch (DataAccessException e) {
        } finally {
            return kecamatans;
        }
    }

    @RequestMapping(value = "/kecamatan.json", method = RequestMethod.POST)
    public
    @ResponseBody
    Map<String, Object> save(@RequestBody Kecamatan kecamatan) {
        if (kecamatan == null) throw new IllegalArgumentException("A Product is required");
        Map<String, Object> result = new HashMap<String, Object>();
        try {
            kecamatanDAO.save(kecamatan);
            result.put("success", true);
            result.put("kecamatan", kecamatan);
            result.put("message", "Sukses Save Kecamatan " + kecamatan);
        } catch (DataAccessException e) {
            result.put("success", false);
            result.put("kecamatan", kecamatan);
            result.put("exception", e.getMessage());
            result.put("message", "Gagal Save Kecamatan " + kecamatan);
        }
        return result;
    }

    @RequestMapping(value = "/kecamatan/{id}/delete.json", method = RequestMethod.GET)
    public
    @ResponseBody
    Map<String, Object> delete(@PathVariable("id") Kecamatan kecamatan) {
        Map<String, Object> result = new HashMap<String, Object>();
        try {
            kecamatanDAO.delete(kecamatan);
            result.put("success", true);
            result.put("message", "Sukses Delete Kecamatan " + kecamatan);
        } catch (DataAccessException e) {
            result.put("success", false);
            result.put("exception", e.getMessage());
            result.put("message", "Gagal Delete Kecamatan " + kecamatan);
        }
        return result;
    }

    @RequestMapping(value = "/kecamatan.json", method = RequestMethod.PUT)
    public
    @ResponseBody
    Map<String, Object> update(@RequestBody Kecamatan kecamatan) {
        if (kecamatan == null) throw new IllegalArgumentException("A Product is required");
        Map<String, Object> result = new HashMap<String, Object>();
        try {
            kecamatanDAO.merge(kecamatan);
            result.put("success", true);
            result.put("kecamatan", kecamatan);
            result.put("message", "Sukses Update Kecamatan " + kecamatan);
        } catch (DataAccessException e) {
            result.put("success", false);
            result.put("kecamatan", kecamatan);
            result.put("exception", e.getMessage());
            result.put("message", "Gagal Update Kecamatan " + kecamatan);
        }
        return result;
    }

    @InitBinder
    protected void initBinder(WebDataBinder binder) throws Exception {
        binder.registerCustomEditor(Kabupaten.class, new KabupatenEditor(kabupatenDAO));
        binder.registerCustomEditor(Kecamatan.class, new KecamatanEditor(kecamatanDAO));
    }

}
